KERNELDIR ?= /home/yzhu/ar9301sw/ar9341-ar-sdk-0.16.01-01-alpha-20240409/base/arsdk/platforms/kernel/linux-linaro-stable-lsk-v4.9-17.07
# CROSS_COMPILE ?= /opt/toolchain/7.5.0/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-

PWD :=$(shell pwd)
ARCH ?=arm64

BIN_NAME ?= artosyn_drv
BINDIR ?= install
MODEXT := ko

ifneq ($(KERNELRELEASE),)
include $(DRV_DIR)/config.mk

OBJS = oal_main.o oal_workqueue.o oal_mdev.o oal_dnld.o
EXTRA_CFLAGS += -I$(DRV_DIR)

#net
OBJS += net/net_dev.o
EXTRA_CFLAGS += -I$(DRV_DIR)/net

#rpc
OBJS += rpc/ar_rpc.o rpc/session_socket.o
EXTRA_CFLAGS += -I$(DRV_DIR)/rpc

#utils
OBJS += utils/utils.o  utils/ringbuffer.o
EXTRA_CFLAGS += -I$(DRV_DIR)/utils

#bus
EXTRA_CFLAGS += -I$(DRV_DIR)/bus

## usb
ifeq ($(CONFIG_BUS_USB), y)
OBJS += bus/usb.o
#EP_TYPE: 2(BULK), 3(INT)
subdir-ccflags-y += -DAR_MSG_EP_TYPE=2
subdir-ccflags-y += -DAR_USB_SUPPORT
endif

## sdio
ifeq ($(CONFIG_BUS_SDIO), y)
OBJS += bus/sdio.o
subdir-ccflags-y += -DAR_SDIO_SUPPORT
endif

#control
OBJS += control/ar_chardev.o

ifeq ($(CONFIG_PROC_FS), y)
OBJS += control/ar_proc.o
subdir-ccflags-$(CONFIG_PROC_FS) += -DENABLE_PROC_FS
endif

EXTRA_CFLAGS += -I$(DRV_DIR)/control

obj-m := $(BIN_NAME).o

$(BIN_NAME)-objs := $(OBJS)

subdir-ccflags-y += -Wno-unused-function -Wno-unused-variable -Wno-declaration-after-statement -Wno-int-conversion
# Otherwise we were called directly from the command line; invoke the kernel build system.
else

DRV_DIR = $(CURDIR)

export DRV_DIR

default:
ifeq ($(CROSS_COMPILE),)
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
else
	$(MAKE) -C $(KERNELDIR) O=$(KBUILDDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
endif
	cp -f $(BIN_NAME).$(MODEXT) $(BIN_NAME)_strip.$(MODEXT)
	$(CROSS_COMPILE)strip --strip-debug $(BIN_NAME)_strip.$(MODEXT)

endif

###############################################################

export		CC LD EXTRA_CFLAGS KERNELDIR

#	echo "Finished Making Artosyn USB Fw Download Driver"

echo:

install: echo default
	@if [ ! -d $(BINDIR) ]; then \
		mkdir $(BINDIR); \
	fi
	cp -f $(BIN_NAME).$(MODEXT) $(BINDIR)/
	cp -f $(BIN_NAME)_strip.$(MODEXT) $(BINDIR)/


clean:
	-find . -name "*.o" -exec rm {} \;
	-find . -name "*.ko" -exec rm {} \;
	-find . -name ".*.cmd" -exec rm {} \;
	-find . -name "*.mod" -exec rm {} \;
	-find . -name "*.mod.c" -exec rm {} \;
	-find . -name "Module.symvers" -exec rm {} \;
	-find . -name "Module.markers" -exec rm {} \;
	-find . -name "modules.order" -exec rm {} \;
	-rm -rf .tmp_versions

distclean:
	-find . -name "*.o" -exec rm {} \;
	-find . -name "*.orig" -exec rm {} \;
	-find . -name "*.swp" -exec rm {} \;
	-find . -name "*.*~" -exec rm {} \;
	-find . -name "*~" -exec rm {} \;
	-find . -name "*.d" -exec rm {} \;
	-find . -name "*.a" -exec rm {} \;
	-find . -name "tags" -exec rm {} \;
	-find . -name ".*" -exec rm -rf 2> /dev/null \;
	-find . -name "*.ko" -exec rm {} \;
	-find . -name ".*.cmd" -exec rm {} \;
	-find . -name "*.mod" -exec rm {} \;
	-find . -name "*.mod.c" -exec rm {} \;
	-rm -rf .tmp_versions

# End of file
